import pygame
from game import Game


class Player2(pygame.sprite.Sprite):
    def __init__(self, left: int, top: int):
        super(Player2, self).__init__()
        self.size_x = 40
        self.size_y = 60
        self.images_left = []
        for i in range(1, 5):
            left_img = pygame.image.load(f"img/player2/走左边/frame-{i}.png")
            image_left = pygame.transform.scale(left_img, (self.size_x, self.size_y))
            self.images_left.append(image_left)

        self.images_right = []
        for i in range(1, 5):
            right_img = pygame.image.load(f"img/player2/走右边/frame-{i}.png")
            image_left = pygame.transform.scale(right_img, (self.size_x, self.size_y))
            self.images_right.append(image_left)

        self.image11 = pygame.image.load(f"img/player2/人物右.png")
        self.image22 = pygame.image.load(f"img/player2/人物左.png")
        self.image33 = pygame.image.load(f"img/player2/人物右.png")

        self.image = pygame.transform.scale(self.image11, (self.size_x, self.size_y))
        self.image2 = pygame.transform.scale(self.image22, (self.size_x, self.size_y))
        self.image3 = pygame.transform.scale(self.image33, (self.size_x, self.size_y))

        self.character_images = {
            "right": pygame.image.load("img/player2/人物右.png"),
            "left": pygame.image.load("img/player2/人物左.png"),
            "jump_right": pygame.image.load("img/player2/人物跳右.png"),
            "jump_left": pygame.image.load("img/player2/人物跳左.png"),
        }
        self.player_image_left = pygame.transform.scale(self.character_images["left"], (self.size_x, self.size_y))
        self.player_image_right = pygame.transform.scale(self.character_images["right"], (self.size_x, self.size_y))
        self.player_image_jump_right = pygame.transform.scale(self.character_images["jump_right"],
                                                              (self.size_x, self.size_y))
        self.player_image_jump_left = pygame.transform.scale(self.character_images["jump_left"],
                                                             (self.size_x, self.size_y))

        self.rect = self.images_left[0].get_rect()
        self.rect.left = left
        self.rect.top = top

        self.player_old_left = 0
        self.player_old_top = 0
        self.player_speed = 6

        self.player_power = 10
        Game.direction2 = "right"
        Game.current_images2 = self.images_left

        self.renwu_font = pygame.font.Font("Font/font.ttf", 14)
        self.score_Player2 = self.renwu_font.render("Player2", True, (0, 250, 0))
        self.color = (255, 0, 255)
        self.box_w = 80
        self.box_h = 16

    def update(self, *args):
        Game.window.fill(self.color, (self.rect.x - 20, self.rect.y - 25, self.box_w, self.box_h))
        Game.window.blit(self.score_Player2, (self.rect.x, self.rect.y - 27))
        self.player_old_top = self.rect.top
        self.player_old_left = self.rect.left
        if Game.direction_W2:
            self.rect.top -= self.player_power

            if self.image == self.player_image_left:
                self.image = self.player_image_jump_left
            elif self.image == self.player_image_right:
                self.image = self.player_image_jump_right

        if Game.direction_D2 and not Game.direction_W2:
            Game.current_images2 = self.images_right
            self.image = Game.current_images2[args[0] % len(Game.current_images2)]

        elif Game.direction_A2 and not Game.direction_W2:
            Game.current_images2 = self.images_left
            self.image = Game.current_images2[args[0] % len(Game.current_images2)]

    def left_right(self):
        self.player_old_top = self.rect.top
        self.player_old_left = self.rect.left
        if Game.direction_D2:
            Game.direction2 = "right"
            self.image = self.image3
            self.rect.left += self.player_speed
        elif Game.direction_A2:
            Game.direction2 = "left"
            self.image = self.image2
            self.rect.left -= self.player_speed
        if Game.direction_W2 and Game.direction_D2:
            self.image = self.player_image_jump_right
        elif Game.direction_W2 and Game.direction_A2:
            self.image = self.player_image_jump_left

        else:
            if self.image == self.player_image_jump_right:
                self.image = self.player_image_right
            elif self.image == self.player_image_jump_left:
                self.image = self.player_image_left

    def confine(self):
        self.player_old_top = self.rect.top
        self.player_old_left = self.rect.left

        if self.rect.x <= 0:
            self.rect.x = 0
        if self.rect.x >= Game.W - self.rect.width:
            self.rect.x = Game.W - self.rect.width
        if self.rect.y <= 0:
            self.rect.y = 0
        if self.rect.y >= Game.H - self.rect.height:
            self.rect.y = Game.H - self.rect.height
            Game.G_speed2 = 0
            Game.direction_W2 = False
            Game.jump2 = 0

    def hit_wall(self):
        for row in range(Game.MAP_HEIGHT):
            for col in range(Game.MAP_WIDTH):
                if Game.wall_list[row][col] in (1, 2, 4):
                    wall_rect = pygame.Rect(col * Game.BLOCK_SIZE, row * Game.BLOCK_SIZE, Game.BLOCK_SIZE,
                                            Game.BLOCK_SIZE)

                    if self.rect.colliderect(wall_rect):
                        if self.rect.left < wall_rect.left:
                            self.rect.left = self.player_old_left

                        elif self.rect.right > wall_rect.right:
                            self.rect.left = self.player_old_left

                    if self.rect.colliderect(wall_rect):
                        if self.rect.top <= wall_rect.bottom:
                            self.rect.top = wall_rect.bottom + 1
                            Game.jump2 = 3

    def player2_G(self):
        Game.G_speed2 += Game.G2  # 增加垂直速度
        self.rect.y += Game.G_speed2  # 根据垂直速度更新位置
